package com.scm.integration;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import scm.classifica.jaxb.CLSBean;
import scm.live.jaxb.CMPBean;
import scm.live.jaxb.LIVE;
import scm.live.jaxb.MATCHTBean;
import scm.quote.jaxb.Match;
import scm.quote.jaxb.SCMBean;
import com.scm.error.SCPException;
import com.scm.export.PDFExport;
import com.scm.export.PDFExportImpl;
import com.scm.parser.QuoteParserJaxb;
import com.scm.parser.StatisticLive;
import com.scm.parser.StatisticParser;
import com.scm.parser.TEXTParserLive;
import com.scm.parser.TEXTParserQuoteJaxb;
import com.scm.parser.TEXTParserStatistic;

public class SCMImpl implements InterfaceSCM {
	
	private static Logger log = Logger.getLogger(SCMImpl.class);
	private SCMBean scmObject =  null;
	

	public SCMBean getQuote() {
		log.info("SCM - GET QUOTE ");
		
		PDFExport pdfEx = new PDFExportImpl();
		QuoteParserJaxb parser = new TEXTParserQuoteJaxb();
		
		scmObject =  null;
  		try {
			pdfEx.exportPDFile();
			scmObject = parser.getBeanScm();
			return scmObject;
  		} catch (SCPException excp) {
			log.error("CODE: "+excp.getErrorCode()+" :: "+excp.getUserMessage()+" - "+excp.getErrorDescription());
		}
  		
		return null;
	}


	public CLSBean getClassifica(String id_Campionato) {
		log.info("SCM - GET CLASSIFICA - "+id_Campionato);
		if(id_Campionato.equals("CHAMPIONS") || id_Campionato.equals("EUROPA LEAGUE")){
			log.warn("NO Implemenrs CHAMPIONS-EUROPA CLASSIFICA!");
			return null;
		}
		StatisticParser statistic = new TEXTParserStatistic(id_Campionato);
		try {
			CLSBean clsObject = statistic.getBeanStatistic();
			return clsObject;
		} catch (SCPException excp) {
			log.error("CODE: "+excp.getErrorCode()+" :: "+excp.getUserMessage()+" - "+excp.getErrorDescription());
		}
		
		return null;
	}


	public List<String> getIdCampionati() {
		if(scmObject == null)
			this.getQuote();
		List<String> listaCampionati =  new ArrayList<String>();
		if( scmObject != null){
			
			for(Match match : scmObject.getMatch()){
				if(  match!= null){
					if(!listaCampionati.contains(match.getTipoAvv())){
						listaCampionati.add(match.getTipoAvv());
					}
				}
			}
		}
		return listaCampionati;
	}


	public List<String> getDate() {
		if(scmObject == null)
			this.getQuote();
		List<String> dataLista =  new ArrayList<String>();
		if( scmObject != null){
		for(Match match : scmObject.getMatch()){
			if(  match!= null){
				if(!dataLista.contains(match.getDataMatch())){
					dataLista.add(match.getDataMatch());
				}
			}
		}
		}
		return dataLista;
	}


	public List<String> getPalinsesto() {
		if(scmObject == null)
			this.getQuote();
		List<String> palinsestoLista =  new ArrayList<String>();
		for(Match match : scmObject.getMatch()){
				if(!palinsestoLista.contains(match.getAvvenimento())){
					palinsestoLista.add(match.getAvvenimento());
				}
		}
		return palinsestoLista;
	}


	public List<MATCHTBean> getLiveScore(CMPBean cmpbean) {
		List<MATCHTBean> listMatch = new ArrayList<MATCHTBean>();
		for(MATCHTBean  match : cmpbean.getMATCHS().getMATCH()){
			match.setCMP(cmpbean.getID());
			listMatch.add(match);
		}
		
		return listMatch;
	}

	public List<CMPBean> getCmpLive() {
		StatisticLive statisticLive= new TEXTParserLive();
		try {
			LIVE live =  statisticLive.getBeanStatistic();
			return live.getCOMPETATION();
		} catch (SCPException excp) {
			log.error("CODE: "+excp.getErrorCode()+" :: "+excp.getUserMessage()+" - "+excp.getErrorDescription());
		}
		return null;
	}

}
